package com.ruoyi.transfers.controller;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.enums.GoodsTransferStatus;
import com.ruoyi.common.enums.RuleType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.exitwarehouse.domain.WExitWarehouseInfo;
import com.ruoyi.fee.domain.WGoodsTransferFeeRule;
import com.ruoyi.fee.service.IWGoodsTransferFeeRuleService;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.domain.TGoodsWeightSum;
import com.ruoyi.goods.domain.WGoodsExt;
import com.ruoyi.goods.domain.WGoodsExtAssist;
import com.ruoyi.goods.service.ITGoodsInfoService;
import com.ruoyi.goods.service.IWGoodsExtAssistService;
import com.ruoyi.goods.service.IWGoodsExtService;
import com.ruoyi.transfers.domain.TimeLineActivity;
import com.ruoyi.transfers.domain.WGoodsTransferAccount;
import com.ruoyi.transfers.service.IWGoodsTransferAccountService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.transfers.domain.WGoodsTransferInfo;
import com.ruoyi.transfers.service.IWGoodsTransferInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 货转信息Controller
 * 
 * @author wzy
 * @date 2024-05-29 10:08:30
 */
@RestController
@RequestMapping("/transfers/transfer")
public class WGoodsTransferInfoController extends BaseController
{
    @Autowired
    private IWGoodsTransferInfoService wGoodsTransferInfoService;

    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private IWGoodsTransferFeeRuleService wGoodsTransferFeeRuleService;

    @Autowired
    private IWGoodsTransferAccountService wGoodsTransferAccountService;

    @Autowired
    private IWGoodsExtService wGoodsExtService;

    @Autowired
    private IWGoodsExtAssistService wGoodsExtAssistService;

    private static Logger logger = LoggerFactory.getLogger(WGoodsTransferInfoController.class);
    /**
     * 查询货转信息列表
     */
    @PreAuthorize("@ss.hasPermi('transfers:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(WGoodsTransferInfo wGoodsTransferInfo)
    {
        startPage();
        List<WGoodsTransferInfo> list = wGoodsTransferInfoService.selectWGoodsTransferInfoList(wGoodsTransferInfo);
        return getDataTable(list);
    }

    /**
     * 导出货转信息列表
     */
    @PreAuthorize("@ss.hasPermi('transfers:transfer:export')")
    @Log(title = "货转信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WGoodsTransferInfo wGoodsTransferInfo)
    {
        List<WGoodsTransferInfo> list = wGoodsTransferInfoService.selectWGoodsTransferInfoList(wGoodsTransferInfo);
        ExcelUtil<WGoodsTransferInfo> util = new ExcelUtil<WGoodsTransferInfo>(WGoodsTransferInfo.class);
        util.exportExcel(response, list, "货转信息数据");
    }

    /**
     * 获取货转信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('transfers:transfer:query')")
    @GetMapping(value = "/{goodsTransferId}")
    public AjaxResult getInfo(@PathVariable("goodsTransferId") Long goodsTransferId)
    {
        return success(wGoodsTransferInfoService.selectWGoodsTransferInfoByGoodsTransferId(goodsTransferId));
    }

    /**
     * 新增货转信息
     */
    @PreAuthorize("@ss.hasPermi('transfers:transfer:add')")
    @Log(title = "货转信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WGoodsTransferInfo wGoodsTransferInfo)
    {
        wGoodsTransferInfo.setGoodsTransferOrderNumber(OrderUtils.getOrderNumber("TO", "-"));
        wGoodsTransferInfo.setGoodsTransferStatus(GoodsTransferStatus.NOT.getCode());
        wGoodsTransferInfo.setCreateBy(getNickName());
        return toAjax(wGoodsTransferInfoService.insertWGoodsTransferInfo(wGoodsTransferInfo));
    }

    /**
     * 修改货转信息
     */
    @PreAuthorize("@ss.hasPermi('transfers:transfer:edit')")
    @Log(title = "货转信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WGoodsTransferInfo wGoodsTransferInfo)
    {
        wGoodsTransferInfo.setUpdateBy(getNickName());
        return toAjax(wGoodsTransferInfoService.updateWGoodsTransferInfo(wGoodsTransferInfo));
    }

    /**
     * 删除货转信息
     */
    @PreAuthorize("@ss.hasPermi('transfers:transfer:remove')")
    @Log(title = "货转信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsTransferIds}")
    public AjaxResult remove(@PathVariable Long[] goodsTransferIds)
    {
        //货物状态，需要改成“已在库”
        tGoodsInfoService.updateTGoodsInventoryStatusByTransferIds(goodsTransferIds, "已在库");
        return toAjax(wGoodsTransferInfoService.deleteWGoodsTransferInfoByGoodsTransferIds(goodsTransferIds));
    }

    /**
     * 修改货转信息
     */
    @PreAuthorize("@ss.hasPermi('transfers:transfer:audit')")
    @Log(title = "货转信息", businessType = BusinessType.AUDIT)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody WGoodsTransferInfo wGoodsTransferInfo)
    {
        Integer rtn = 0;
        String transferOrderNumber = wGoodsTransferInfo.getGoodsTransferOrderNumber();
        Date transferTime = wGoodsTransferInfo.getGoodsTransferTime();
        String transferUserName = wGoodsTransferInfo.getCreateBy();
        Date auditTime = DateUtils.getNowDate();
        String auditUserName = getNickName();
        Double goodsTransferWeight = 0.0;
        if(wGoodsTransferInfo.getOriginTransferStatus().equals(wGoodsTransferInfo.getGoodsTransferStatus())){
            return new AjaxResult(500, "相同操作不能重复执行！");
        }
        //logger.debug("BBB " + "wGoodsTransferInfo = " + wGoodsTransferInfo);
        if(StringUtils.isNotEmpty(wGoodsTransferInfo.getGoodsType()) && wGoodsTransferInfo.getGoodsType().equals("煤炭"))
        {
            if (wGoodsTransferInfo.getGoodsTransferStatus().equals(GoodsTransferStatus.PASS.getCode())) {
                handleMineInventory(BusinessType.CONFIRM, wGoodsTransferInfo);
                handleMineGoodsTransferAccount(BusinessType.CONFIRM, wGoodsTransferInfo);
            } else {
                handleMineInventory(BusinessType.REVOKE, wGoodsTransferInfo);
                handleMineGoodsTransferAccount(BusinessType.REVOKE, wGoodsTransferInfo);
            }
        } else {
            if (wGoodsTransferInfo.getGoodsTransferStatus().equals(GoodsTransferStatus.PASS.getCode())) {
                boolean checkRightOk = true;
                StringBuilder sb = new StringBuilder();
                String originGoodsRightCustomer = wGoodsTransferInfo.getOriginGoodsRightCustomer();
                String newGoodsRightCustomer = wGoodsTransferInfo.getNewGoodsRightCustomer();
//            logger.debug("BBB " + "wGoodsTransferInfo = " + wGoodsTransferInfo);

                List<TGoodsInfo> list = tGoodsInfoService.selectTGoodsInfoByTransferId(wGoodsTransferInfo.getGoodsTransferId());

                sb.append("部分货物{");

                for (TGoodsInfo item : list) {
                    if (item.getGoodsRightCustomer() == null || item.getGoodsRightCustomer().isEmpty()) {
                        checkRightOk = false;
                        sb.append(item.getBundleNumber());
                        sb.append(", ");
                    } else {
                        if (!item.getGoodsRightCustomer().equals(originGoodsRightCustomer)) {
                            checkRightOk = false;
                            sb.append(item.getBundleNumber());
                            sb.append(", ");
                        }
                    }

                    item.setOriginGoodsRightCustomer(originGoodsRightCustomer);
                    item.setGoodsRightCustomer(newGoodsRightCustomer);
                    item.setGoodsTransferTimes(item.getGoodsTransferTimes() + 1);
//                logger.debug("BBB " + "item.getGoodsTransferIds() = " + item.getGoodsTransferIds());
                    if (item.getGoodsTransferIds() == null || item.getGoodsTransferIds().isEmpty()) {
                        item.setGoodsTransferIds(wGoodsTransferInfo.getGoodsTransferId().toString() + ",");
                    } else {
                        item.setGoodsTransferIds(item.getGoodsTransferIds() + wGoodsTransferInfo.getGoodsTransferId().toString() + ",");
                    }
                    item.setGoodsTransferOrderNumber(transferOrderNumber);
                    item.setGoodsTransferPerson(transferUserName);
                    item.setGoodsTransferTime(transferTime);
                    item.setGoodsTransferAuditPerson(auditUserName);
                    item.setGoodsTransferAuditTime(auditTime);
                    goodsTransferWeight = goodsTransferWeight + item.getWeight();
                    item.setInventoryStatus("已在库");

                }
                sb.append("}货权方不是");
                sb.append(originGoodsRightCustomer);
                if (!checkRightOk)
                    return new AjaxResult(500, sb.toString());

                rtn = tGoodsInfoService.batchUpdateTGoodsInfo(list);
//            logger.debug("CCC " + "rtn = " + rtn);
                //货转重量，保留3位小数
                goodsTransferWeight = Math.round(goodsTransferWeight * 1000.0) / 1000.0;
                wGoodsTransferInfo.setGoodsTransferWeight(goodsTransferWeight);

                //Goods Transfer Account
                rtn = handleGoodsTransferAccount(wGoodsTransferInfo, list);
//            logger.debug("CCC " + "handleGoodsTransferAccount rtn = " + rtn);
            } else {
//            logger.debug("DDD " + "退回 ");
//            logger.debug("EEE " + "wGoodsTransferInfo.getOriginTransferStatus = " + wGoodsTransferInfo.getOriginTransferStatus());
                if (wGoodsTransferInfo.getOriginTransferStatus().equals(GoodsTransferStatus.PASS.getCode())) {
                    List<TGoodsInfo> list = tGoodsInfoService.selectTGoodsInfoByTransferId(wGoodsTransferInfo.getGoodsTransferId());
                    String originGoodsRightCustomer = wGoodsTransferInfo.getOriginGoodsRightCustomer();

                    for (TGoodsInfo item : list) {
                        item.setGoodsRightCustomer(originGoodsRightCustomer);
                        item.setGoodsTransferIds(item.getGoodsTransferIds().replace(wGoodsTransferInfo.getGoodsTransferId().toString() + ",", ""));
                        item.setGoodsTransferTimes(item.getGoodsTransferTimes() - 1);
                        item.setGoodsTransferPerson(transferUserName);
                        item.setGoodsTransferTime(transferTime);
                        item.setGoodsTransferAuditPerson(auditUserName);
                        item.setGoodsTransferAuditTime(auditTime);
                    }
                    rtn = tGoodsInfoService.batchUpdateTGoodsInfo(list);
                }
            }
        }
//        logger.debug("CCC " + "rtn = " + rtn);

        wGoodsTransferInfo.setGoodsTransferAuditPerson(getNickName());
        wGoodsTransferInfo.setGoodsTransferAuditTime(DateUtils.getNowDate());
        wGoodsTransferInfo.setUpdateBy(getNickName());
        return toAjax(wGoodsTransferInfoService.updateWGoodsTransferInfo(wGoodsTransferInfo));
    }

    private Integer handleGoodsTransferAudit(WGoodsTransferInfo wGoodsTransferInfo)
    {
        return 0;
    }

    private Integer handleGoodsTransferAccount(WGoodsTransferInfo wGoodsTransferInfo, List<TGoodsInfo> list)
    {
        Double goodsTransferChargeWeight = 0.0;
        Integer rtn = 0;

        WGoodsTransferAccount goodsTransferAccount = new WGoodsTransferAccount();
        goodsTransferAccount.setGoodsTransferAccountNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber().replace("TO","TA"));
        goodsTransferAccount.setGoodsTransferId(wGoodsTransferInfo.getGoodsTransferId());
        goodsTransferAccount.setGoodsTransferOrderNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber());
        goodsTransferAccount.setOriginGoodsRightCustomer(wGoodsTransferInfo.getOriginGoodsRightCustomer());
        goodsTransferAccount.setNewGoodsRightCustomer(wGoodsTransferInfo.getNewGoodsRightCustomer());

        goodsTransferAccount.setGoodsTransferStatus(wGoodsTransferInfo.getGoodsTransferStatus());
        goodsTransferAccount.setGoodsTransferTime(wGoodsTransferInfo.getGoodsTransferTime());
        goodsTransferAccount.setGoodsTransferPerson(wGoodsTransferInfo.getCreateBy());
        goodsTransferAccount.setGoodsTransferWeight(wGoodsTransferInfo.getGoodsTransferWeight());
        goodsTransferAccount.setNumberOfPiece(list.size());
        goodsTransferAccount.setGoodsTransferPicture(wGoodsTransferInfo.getGoodsTransferPicture());
        goodsTransferAccount.setGoodsTransferSettlementCustomer(wGoodsTransferInfo.getGoodsTransferSettlementCustomer());
        goodsTransferAccount.setGoodsTransferSettlementMode(wGoodsTransferInfo.getGoodsTransferSettlementMode());

        //原货主付货转费
        List<WGoodsTransferFeeRule> goodsTransferFeeList = wGoodsTransferFeeRuleService.selectWGoodsTransferFeeRuleByCustomerName(goodsTransferAccount.getGoodsTransferSettlementCustomer());
//        logger.debug("DDD " + "goodsTransferFeeList = " + goodsTransferFeeList);
        Double goodsTransferPrice = 0.0;
        Integer goodsTransferFreeTimes = 0;
        if(goodsTransferFeeList.size() > 0) {
            for (WGoodsTransferFeeRule item : goodsTransferFeeList) {
                if (item.getRuleType().equals(RuleType.COMMON.getInfo())) {
                    goodsTransferPrice = item.getGoodsTransferPrice();
                    goodsTransferFreeTimes = item.getGoodsTransferFreeTimes();
                }
                if (item.getRuleType().equals(RuleType.CUSTOMIZED.getInfo())) {
                    goodsTransferPrice = item.getGoodsTransferPrice();
                    goodsTransferFreeTimes = item.getGoodsTransferFreeTimes();
                }
            }

            for (TGoodsInfo item : list) {
                if (item.getGoodsTransferTimes() > goodsTransferFreeTimes) {
                    goodsTransferChargeWeight = goodsTransferChargeWeight + item.getWeight();
                }
            }
            goodsTransferAccount.setGoodsTransferFreeTimes(goodsTransferFreeTimes);
            goodsTransferAccount.setGoodsTransferPrice(goodsTransferPrice);
            goodsTransferAccount.setGoodsTransferChargeWeight(goodsTransferChargeWeight);
            goodsTransferAccount.setGoodsTransferDiscountAmount(0.0);
            //DecimalFormat df3 = new DecimalFormat("#.000");
            goodsTransferAccount.setGoodsTransferChargeWeight(Math.round(goodsTransferChargeWeight * 1000.0) / 1000.0);
            //DecimalFormat df2 = new DecimalFormat("#.00");
            goodsTransferAccount.setGoodsTransferFee(Math.round(goodsTransferPrice * goodsTransferChargeWeight * 100.0) / 100.0);
            goodsTransferAccount.setCreateBy(getNickName());

//            logger.debug("FFF " + "goodsTransferAccount = " + goodsTransferAccount);

            rtn = wGoodsTransferAccountService.deleteWGoodsTransferAccountByGoodsTransferAccountNumber(goodsTransferAccount.getGoodsTransferAccountNumber());
//            logger.debug("GGG " + "rtn = " + rtn);
            rtn = wGoodsTransferAccountService.insertWGoodsTransferAccount(goodsTransferAccount);
//            logger.debug("HHH " + "rtn = " + rtn);
        } else {
            logger.debug("EEE " + "goodsTransferFeeList.size() = " + goodsTransferFeeList.size());
//            return 0;
        }

        return rtn;

    }
    /**
     * 货转时间线信息
     */
    @PreAuthorize("@ss.hasPermi('transfers:trace:track')")
//    @Log(title = "货转信息", businessType = BusinessType.DELETE)
    @GetMapping("/trace/{goodsTransferIds}")
    public AjaxResult trace(@PathVariable String goodsTransferIds)
//    public AjaxResult trace(@PathVariable Long[] goodsTransferIds)
    {
        List<TimeLineActivity> list = wGoodsTransferInfoService.selectTimeLineActivityByGoodsTransferId(goodsTransferIds);
//        logger.debug("FFF " + "rtn = " + list);
        return success(list);
    }

    /**
     * 获取货物重量合计
     */
    //@PreAuthorize("@ss.hasPermi('goods:good:query')")
    @GetMapping(value = "/transferweightsum")
    public AjaxResult transferWeightSum(WGoodsTransferInfo wGoodsTransferInfo)
    {
        List<TGoodsWeightSum> list = wGoodsTransferInfoService.getWeightSumByGoodsTransferInfo(wGoodsTransferInfo);
//        logger.debug("GGG " + "getWeightSumByGoodsTransferInfo = " + list);
        return success(list);
    }
    /**
     * 处理煤炭库存
     * @param operate
     * @param wGoodsTransferInfo
     */
    private void handleMineInventory(BusinessType operate, WGoodsTransferInfo wGoodsTransferInfo)
    {
        WGoodsExt wGoodsExt = new WGoodsExt();
        wGoodsExt.setGoodsType(wGoodsTransferInfo.getGoodsType());
        wGoodsExt.setProduct(wGoodsTransferInfo.getProduct());
        wGoodsExt.setSpecification(wGoodsTransferInfo.getSpecification());
        //wGoodsExt.setSteelGrade(wGoodsTransferInfo.getSteelGrade());

        wGoodsExt.setLogisticsParkName(wGoodsTransferInfo.getLogisticsParkName());
        wGoodsExt.setWarehouseLocateName(wGoodsTransferInfo.getWarehouseLocateName());
        wGoodsExt.setRegionLocateName(wGoodsTransferInfo.getRegionLocateName());
        //wGoodsExt.setGoodsLocateName(wEnterWarehouseInfo.getGoodsLocateName());
        //wGoodsExt.setLayerLocateName(wEnterWarehouseInfo.setLayerLocateName());

        wGoodsExt.setGoodsRightCustomer(wGoodsTransferInfo.getOriginGoodsRightCustomer());

        Integer record = wGoodsExtService.selectWGoodsExtRecordCountByGoodsExt(wGoodsExt);
        //logger.debug("GGG " + "handleMineInventory record = " + record);
        /**
         * 确认货转
         */
        if(BusinessType.CONFIRM == operate) {
            String newBuddleNumber, newBuddleNumberAlias;
            //logger.debug("GGG " + "handleMineInventory wExitWarehouseInfo = " + wExitWarehouseInfo);
            if(record > 0) {
                /**
                 * 扣减库存
                 */
                WGoodsExt goodsExt = wGoodsExtService.selectWGoodsExtOneByGoodsExt(wGoodsExt);
                double weight = goodsExt.getWeight();
                weight = weight - wGoodsTransferInfo.getGoodsTransferWeight();
                weight = Math.round(weight * 1000.0) / 1000.0;
                goodsExt.setWeight(weight);
                wGoodsExtService.updateWGoodsExt(goodsExt);

                /**
                 * 增加库存
                 */
                wGoodsExt.setGoodsRightCustomer(wGoodsTransferInfo.getNewGoodsRightCustomer());
                WGoodsExt newGoodsExt = wGoodsExtService.selectWGoodsExtOneByGoodsExt(wGoodsExt);
                if(StringUtils.isNotNull(newGoodsExt)) {
                    newBuddleNumber = newGoodsExt.getBundleNumber();
                    newBuddleNumberAlias = newGoodsExt.getBundleNumberAlias();
                    weight = newGoodsExt.getWeight();
                    weight = weight + wGoodsTransferInfo.getGoodsTransferWeight();
                    weight = Math.round(weight * 1000.0) / 1000.0;
                    newGoodsExt.setWeight(weight);
                    wGoodsExtService.updateWGoodsExt(newGoodsExt);
                } else {
//                    newGoodsExt.setGoodsExtId(null);
                    newBuddleNumber = OrderUtils.getOrderNumber("GBE", "-");
                    newGoodsExt.setBundleNumber(newBuddleNumber);
                    newBuddleNumberAlias = wGoodsTransferInfo.getNewGoodsRightCustomer().substring(0, 4) + wGoodsTransferInfo.getWarehouseLocateName() + wGoodsTransferInfo.getRegionLocateName();
                    newGoodsExt.setBundleNumberAlias(newBuddleNumberAlias);
                    newGoodsExt.setWeight(wGoodsTransferInfo.getGoodsTransferWeight());
                    newGoodsExt.setLogisticsParkName(wGoodsTransferInfo.getLogisticsParkName());
                    newGoodsExt.setWarehouseLocateName(wGoodsTransferInfo.getWarehouseLocateName());
                    newGoodsExt.setRegionLocateName(wGoodsTransferInfo.getRegionLocateName());
                    wGoodsExtService.insertWGoodsExt(wGoodsExt);
                }
                /**
                 * 辅助，切库存
                 */
                double exitRemaindWeight = wGoodsTransferInfo.getGoodsTransferWeight();
                //logger.debug("GGG " + "handleMineInventory wExitWarehouseInfo = " + wExitWarehouseInfo);
                //先取一次入库，看重量
                WGoodsExtAssist goodsExtAssist;
                do {
                    goodsExtAssist = wGoodsExtAssistService.selectWGoodsExtAssistOneByEnterWarehouseTimeAsc(goodsExt.getBundleNumber(), "已在库");
                    if(StringUtils.isNotNull(goodsExtAssist))
                    {
                        double tempRemaindWeight = exitRemaindWeight;
                        double remaindWarehouseWeight =  goodsExtAssist.getRemaindWarehouseWeight();
                        exitRemaindWeight = exitRemaindWeight - remaindWarehouseWeight;
                        logger.debug("XXX " + "handleMineInventory exitRemaindWeight = " + exitRemaindWeight);
                        //logger.debug("GGG " + "handleMineInventory goodsExtAssist = " + goodsExtAssist);
                        if(exitRemaindWeight < 0.0)
                        {
//                            logger.debug("GGG " + "handleMineInventory exitRemaindWeight = " + exitRemaindWeight);
//                            logger.debug("GGG " + "handleMineInventory exitRemaindWeight = " + goodsExtAssist.getGoodsExtAssistId());
                            goodsExtAssist.setExitWarehouseMark("货转拆分");
                            goodsExtAssist.setInventoryStatus("已在库");
                            goodsExtAssist.setRemaindWarehouseWeight(remaindWarehouseWeight - tempRemaindWeight);
                            //goodsExtAssist.setGoodsTransferNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber());
                            //goodsExtAssist.setGoodsTransferTime(wGoodsTransferInfo.getGoodsTransferTime());
                            //goodsExtAssist.setGoodsTransferWeight(tempRemaindWeight);
                            //goodsExtAssist.setGoodsRightCustomer(wGoodsTransferInfo.getNewGoodsRightCustomer());
                            goodsExtAssist.setGoodsRightCustomer(wGoodsTransferInfo.getOriginGoodsRightCustomer());
                            wGoodsExtAssistService.updateWGoodsExtAssistGoodsTransferRelFields(goodsExtAssist);
                            //二分拆解，未出库
                            double enterWeight = (Math.round((tempRemaindWeight) * 1000.0) / 1000.0);
                            goodsExtAssist.setBundleNumber(newBuddleNumber);
                            goodsExtAssist.setBundleNumberAlias(newBuddleNumberAlias);
                            goodsExtAssist.setInventoryStatus("已在库");
                            goodsExtAssist.setParentId(goodsExtAssist.getGoodsExtAssistId());
                            goodsExtAssist.setGoodsExtAssistId(null);
                            goodsExtAssist.setLevelMark("1");
                            goodsExtAssist.setExitWarehouseMark("货转入库");
                            goodsExtAssist.setWeight(enterWeight);
                            goodsExtAssist.setGrossWeight(0.0);
                            goodsExtAssist.setTareWeight(0.0);
                            goodsExtAssist.setNetWeight(0.0);
                            goodsExtAssist.setDeductWeight(0.0);
                            goodsExtAssist.setEnterWarehouseWeight(enterWeight);
                            goodsExtAssist.setRemaindWarehouseWeight(enterWeight);
                            goodsExtAssist.setExitWarehouseNumber(null);
                            goodsExtAssist.setExitWarehouseTime(null);
                            goodsExtAssist.setExitWarehouseWeight(0.0);
                            goodsExtAssist.setGoodsTransferNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber());
                            goodsExtAssist.setGoodsTransferWeight(enterWeight);
                            goodsExtAssist.setGoodsTransferTime(wGoodsTransferInfo.getGoodsTransferTime());
                            goodsExtAssist.setUpdateBy("");
                            goodsExtAssist.setUpdateTime(null);
                            goodsExtAssist.setGoodsRightCustomer(wGoodsTransferInfo.getNewGoodsRightCustomer());

                            wGoodsExtAssistService.insertWGoodsExtAssist(goodsExtAssist);
                            break;
                        } else {
//                            logger.debug("GGG else " + "handleMineInventory exitRemaindWeight = " + exitRemaindWeight);
//                            logger.debug("GGG " + "handleMineInventory exitRemaindWeight = " + goodsExtAssist.getGoodsExtAssistId());
                            goodsExtAssist.setBundleNumber(newBuddleNumber);
                            goodsExtAssist.setBundleNumberAlias(newBuddleNumberAlias);
                            goodsExtAssist.setExitWarehouseMark("货转入库");
                            goodsExtAssist.setInventoryStatus("已在库");
                            //goodsExtAssist.setRemaindWarehouseWeight(0.0);
                            goodsExtAssist.setGoodsTransferNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber());
                            goodsExtAssist.setGoodsTransferTime(wGoodsTransferInfo.getGoodsTransferTime());
                            goodsExtAssist.setGoodsTransferWeight(goodsExtAssist.getRemaindWarehouseWeight());
                            goodsExtAssist.setGoodsRightCustomer(wGoodsTransferInfo.getNewGoodsRightCustomer());
                            wGoodsExtAssistService.updateWGoodsExtAssistGoodsTransferRelFields(goodsExtAssist);
                        }
                    } else {
                        logger.error("WExitWarehouseInfoController, Confirm exit warehouse, Operate Assist Inventory Failure !");
                        break;
                    }
                } while(exitRemaindWeight > 0.0);

            } else {
                logger.error("WExitWarehouseInfoController, Confirm exit warehouse, Subtract Inventory Failure !");
            }
        }

        /**
         * 撤销货转
         */
        if(BusinessType.REVOKE == operate)
        {
            /**
             * 还原库存
             */
            if(record > 0) {
                WGoodsExt goodsExt = wGoodsExtService.selectWGoodsExtOneByGoodsExt(wGoodsExt);
                double weight = goodsExt.getWeight();
                weight = weight + wGoodsTransferInfo.getGoodsTransferWeight();
                weight = Math.round(weight * 1000.0) / 1000.0;
                goodsExt.setWeight(weight);
                wGoodsExtService.updateWGoodsExt(goodsExt);
                //二分还原
                List<WGoodsExtAssist> goodsExtAssists = wGoodsExtAssistService.selectWGoodsExtAssistListByGoodsTranserNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber(), "已在库");
                for(WGoodsExtAssist goodsExtAssist : goodsExtAssists)
                {
                    goodsExtAssist.setBundleNumber(goodsExt.getBundleNumber());
                    goodsExtAssist.setBundleNumberAlias(goodsExt.getBundleNumberAlias());
                    goodsExtAssist.setExitWarehouseMark("转撤入库");
                    goodsExtAssist.setInventoryStatus("已在库");
                    //double remaindWarehouseWeight = goodsExtAssist.getEnterWarehouseWeight() - goodsExtAssist.getRemaindWarehouseWeight();
                    //logger.debug("YYY " + "handleMineInventory exitRemaindWeight = " + remaindWarehouseWeight);
                    goodsExtAssist.setRemaindWarehouseWeight(goodsExtAssist.getGoodsTransferWeight());
                    goodsExtAssist.setGoodsTransferNumber(null);
                    goodsExtAssist.setGoodsTransferTime(null);
                    goodsExtAssist.setGoodsTransferWeight(0.0);
                    goodsExtAssist.setGoodsRightCustomer(wGoodsTransferInfo.getOriginGoodsRightCustomer());
                    wGoodsExtAssistService.updateWGoodsExtAssistGoodsTransferRelFields(goodsExtAssist);
                }

                /**
                 * 新货权用户扣减库存
                 */
                wGoodsExt.setGoodsRightCustomer(wGoodsTransferInfo.getNewGoodsRightCustomer());
                goodsExt = wGoodsExtService.selectWGoodsExtOneByGoodsExt(wGoodsExt);
                weight = goodsExt.getWeight();
                weight = weight - wGoodsTransferInfo.getGoodsTransferWeight();
                weight = Math.round(weight * 1000.0) / 1000.0;
                goodsExt.setWeight(weight);
                wGoodsExtService.updateWGoodsExt(goodsExt);

            } else {
                logger.error("WExitWarehouseInfoController, Revoke exit warehouse, Increase Inventory Failure !");
            }
        }
    }

    private Integer handleMineGoodsTransferAccount(BusinessType operate, WGoodsTransferInfo wGoodsTransferInfo)
    {
        Integer rtn = 0;
        /**
         * 确认货转
         */
        if(BusinessType.CONFIRM == operate)
        {
            Double goodsTransferChargeWeight = 0.0;

            WGoodsTransferAccount goodsTransferAccount = new WGoodsTransferAccount();
            goodsTransferAccount.setGoodsTransferAccountNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber().replace("TO","TA"));
            goodsTransferAccount.setGoodsTransferId(wGoodsTransferInfo.getGoodsTransferId());
            goodsTransferAccount.setGoodsTransferOrderNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber());
            goodsTransferAccount.setOriginGoodsRightCustomer(wGoodsTransferInfo.getOriginGoodsRightCustomer());
            goodsTransferAccount.setNewGoodsRightCustomer(wGoodsTransferInfo.getNewGoodsRightCustomer());

            goodsTransferAccount.setGoodsTransferStatus(wGoodsTransferInfo.getGoodsTransferStatus());
            goodsTransferAccount.setGoodsTransferTime(wGoodsTransferInfo.getGoodsTransferTime());
            goodsTransferAccount.setGoodsTransferPerson(wGoodsTransferInfo.getCreateBy());
            goodsTransferAccount.setGoodsTransferWeight(wGoodsTransferInfo.getGoodsTransferWeight());
            goodsTransferAccount.setNumberOfPiece(goodsTransferAccount.getNumberOfPiece());
            goodsTransferAccount.setGoodsTransferPicture(wGoodsTransferInfo.getGoodsTransferPicture());
            goodsTransferAccount.setGoodsTransferSettlementCustomer(wGoodsTransferInfo.getGoodsTransferSettlementCustomer());
            goodsTransferAccount.setGoodsTransferSettlementMode(wGoodsTransferInfo.getGoodsTransferSettlementMode());

            //原货主付货转费
            List<WGoodsTransferFeeRule> goodsTransferFeeList = wGoodsTransferFeeRuleService.selectWGoodsTransferFeeRuleByCustomerName(goodsTransferAccount.getGoodsTransferSettlementCustomer());
//        logger.debug("DDD " + "goodsTransferFeeList = " + goodsTransferFeeList);
            Double goodsTransferPrice = 0.0;
            Integer goodsTransferFreeTimes = 0;
            if(goodsTransferFeeList.size() > 0) {
                for (WGoodsTransferFeeRule item : goodsTransferFeeList) {
                    if (item.getRuleType().equals(RuleType.COMMON.getInfo())) {
                        goodsTransferPrice = item.getGoodsTransferPrice();
                        goodsTransferFreeTimes = item.getGoodsTransferFreeTimes();
                    }
                    if (item.getRuleType().equals(RuleType.CUSTOMIZED.getInfo())) {
                        goodsTransferPrice = item.getGoodsTransferPrice();
                        goodsTransferFreeTimes = item.getGoodsTransferFreeTimes();
                    }
                }

                /*for (TGoodsInfo item : list) {
                    if (item.getGoodsTransferTimes() > goodsTransferFreeTimes) {
                        goodsTransferChargeWeight = goodsTransferChargeWeight + item.getWeight();
                    }
                }*/
                goodsTransferChargeWeight =wGoodsTransferInfo.getGoodsTransferWeight();
                goodsTransferAccount.setGoodsTransferFreeTimes(goodsTransferFreeTimes);
                goodsTransferAccount.setGoodsTransferPrice(goodsTransferPrice);
                goodsTransferAccount.setGoodsTransferChargeWeight(goodsTransferChargeWeight);
                goodsTransferAccount.setGoodsTransferDiscountAmount(0.0);
                //DecimalFormat df3 = new DecimalFormat("#.000");
                goodsTransferAccount.setGoodsTransferChargeWeight(Math.round(goodsTransferChargeWeight * 1000.0) / 1000.0);
                //DecimalFormat df2 = new DecimalFormat("#.00");
                goodsTransferAccount.setGoodsTransferFee(Math.round(goodsTransferPrice * goodsTransferChargeWeight * 100.0) / 100.0);
                goodsTransferAccount.setCreateBy(getNickName());

//            logger.debug("FFF " + "goodsTransferAccount = " + goodsTransferAccount);

                rtn = wGoodsTransferAccountService.deleteWGoodsTransferAccountByGoodsTransferAccountNumber(goodsTransferAccount.getGoodsTransferAccountNumber());
//            logger.debug("GGG " + "rtn = " + rtn);
                rtn = wGoodsTransferAccountService.insertWGoodsTransferAccount(goodsTransferAccount);
//            logger.debug("HHH " + "rtn = " + rtn);
            } else {
                logger.debug("EEE " + "goodsTransferFeeList.size() = " + goodsTransferFeeList.size());
//            return 0;
            }
        }
        /**
         * 撤销货转
         */
        if(BusinessType.REVOKE == operate)
        {
            rtn = wGoodsTransferAccountService.deleteWGoodsTransferAccountByGoodsTransferAccountNumber(wGoodsTransferInfo.getGoodsTransferOrderNumber().replace("TO","TA"));
        }
        return rtn;
    }

}
